/*
 * Copyright (c) Extreme Vision Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef TRT_BASE_H
#define TRT_BASE_H

#include <NvInfer.h>
#include <NvOnnxParser.h>
#include <cuda_runtime.h>
#include <dlfcn.h>
#include <fstream>
#include <iostream>
#include <mutex>

#include "model_handle.h"

#include "evdeploy/core/ev_common_data.h"
#include "evdeploy/runtime/trt/logging.h"
#include "version_config.h"

using namespace nvinfer1;

namespace ev
{
#define CREATE_INFER_INS(ENGINE)                                                                                       \
    extern "C" void *create_infer_ins() { return new ENGINE; }
#define DESTROY_INFER_INS(ENGINE)                                                                                      \
    extern "C" void destroy_infer_ins(void *ins)                                                                       \
    {                                                                                                                  \
        ENGINE *t_ins = (ENGINE *)ins;                                                                                 \
        delete t_ins;                                                                                                  \
    }

    typedef void *(PARSER_CREATOR)(void *network_, void *logger_, int version);
    typedef void *(INFER_RUNTIME_CREATOR)(void *logger, int32_t version);
    typedef void *(INFER_BUILDER_CREATOR)(void *logger, int32_t version);

    class EVBaseTrtModel
    {
      public:
        EVBaseTrtModel() {}

        virtual ~EVBaseTrtModel() {}

        // load model and initization
        virtual EVStatus Init(const char *net, void **config) = 0;

        // relsease source
        virtual EVStatus UnInit() = 0;

        // run optimize, like tensorrt optimize
        virtual EVStatus OptimizeModel(const char *model_name) = 0;

        // run inference
        virtual EVStatus Infer(EVModelData *in, EVModelData *out) = 0;

        virtual EVStatus SetIO(const std::vector<std::string> &inputs, const std::vector<std::string> &outputs) = 0;

      protected:
        bool m_init_flag = false;
    };

    class EVTrtModel
    {
      public:
        EVTrtModel();

        virtual ~EVTrtModel();

      protected:
        ev::EVCudaEngine *m_ev_engine = nullptr;
        ev::EVExecutionContext *m_ev_context = nullptr;
        cudaStream_t m_cuda_stream;

      protected:
        /**
         * @brief load trt engine model
         * @param[in] str_trt_name: trt engine file name
         * @return
         */
        void LoadTrt(const std::string &str_trt_name);
        /**
         * @brief convert onnx format model to trt engine, the onnx model is supposed in FP32/FP16 format, the default
         * conversion data format is FP16 if the device is supported.
         * @param[in] str_onnx_name: onnx model file name
         * @return
         */
        void ConvertEngine(const std::string str_onnx_name);
    };

} // namespace ev
#endif
